var $ = require("../../utils/Ajax.js");
Page({
  data: {
    Id: null,
    Name: null,
    Phone: null,
    Address: null,
    IsDefault: null,
    DefaultStatus: null
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "编辑收货地址"
    }) 
    this.setData({
      Id: options.Id,
      Name: options.Name,
      Phone: options.Phone,
      Address: options.Address
    })
    if (options.IsDefault == 1){
      this.setData({
        IsDefault: true,
        DefaultStatus: 1
      })
    }else{
      this.setData({
        IsDefault: false,
        DefaultStatus: 0
      })
    }
  },

  onShow: function () {

  },

  // 编辑用户姓名
  UserName: function(e){
    var UserName = e.detail.value;
    this.setData({
      Name: UserName
    })
  },

  // 编辑联系电话
  UserPhone: function(e){
    var UserPhone = e.detail.value;
    this.setData({
      Phone: UserPhone
    })
  },

  // 编辑详细收货地址
  UserAddress: function(e){
    var UserAddress = e.detail.value;
    this.setData({
      Address: UserAddress
    })
  },

  // 是否为默认收货地址
  listenerSwitch:function(e){
    var IsDefault = e.detail.value;
    if (IsDefault){
      this.setData({
        DefaultStatus: 1
      })
    }else{
      this.setData({
        DefaultStatus: 0
      })
    }
  },

  // 删除收货地址
  DelectAddress: function(res){
    var that = this;
    $.Ajax("api/delAddress","POST",{
      id: that.data.Id
    },function(res){
      wx.showToast({
        title: res.m,
        icon: 'none',
        duration: 1500,
        mask: true
      })
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        })
      }, 2000)
    })
  },

  // 点击提交
  AddressSubmit: function(){
    var that = this;
    var Id = that.data.Id;
    var Name = that.data.Name;
    var Phone = that.data.Phone;
    var Address = that.data.Address;
    var IsDefault = that.data.DefaultStatus;
    if (Id == '' || Id == null || Name == '' || Name == null || Phone == '' || Phone == null || Address == '' || Address == null || IsDefault == '' || IsDefault == null){
      wx.showToast({
        title: "必填信息不能为空",
        icon: 'none',
        duration: 1500,
        mask: true
      })
    }else{
      $.Ajax("api/doEditAddress", "POST", {
        id: Id,
        name: Name,
        mobile: Phone,
        address: Address,
        isDefault: IsDefault
      }, function (res) {
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 1500,
          mask: true
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
      })
    }
  }
})